package openbang.chatroom.web.widget;


import openbang.chatroom.service.vo.ChatGroup;

import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 
 * 弹出处理框
 * 
 * 支持：加入聊天室
 *       创建聊天室
 * 
 * @author aibozeng
 *
 */

public class ChatRoomInfoDialog  extends DialogBox{
	//根
	private VerticalPanel vPanelRoot = new VerticalPanel();
	//信息区域
	private Grid  vInfo = new Grid(3,3);
	
	//按钮栏 
	private HorizontalPanel vPanelButton = new HorizontalPanel();
	private Button confirmButton = new Button("确定");
	private Button closeButton = new Button("关闭");		
	
	private String actionType = "join" ; // create
	private ChatGroup group = null;
	private String defaultNiceName = "";
	
	private OpenBangChatHandler chatHandler = null;
	
	public ChatRoomInfoDialog(){		
    	setText("对话框");
    	setWidget(vPanelRoot);
    	vPanelRoot.setPixelSize(430, 150);
    	vPanelRoot.add(vInfo);
    	vPanelRoot.add(vPanelButton);
    	vPanelButton.add(confirmButton);
    	vPanelButton.add(closeButton);
    	setAnimationEnabled(true);
	    setGlassEnabled(true);
	    closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
			}
		});
	    confirmButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(getInputPwd()==null){
					((TextBox)vInfo.getWidget(1, 1)).setText("");
				}
				//根据类型校验录入
				if(getInputNiceName()==null || getInputNiceName().trim().length()<=0){
					Window.alert("昵称不能为空");					
					return;
				}
				if(getInputNiceName().trim().length()>=8){
					Window.alert("昵称不能超过8个汉字");					
					return;
				}
				if(actionType.equals("create")){
					if(getInputSubject()==null || getInputSubject().trim().length()<=0){
						Window.alert("主题不能为空");					
						return;
					}
					if(getInputSubject().trim().length()>=8){
						Window.alert("主题不能超过8个汉字");					
						return;
					}
					group.setNiceName(getInputNiceName().trim());
					group.setPassword(getInputPwd().trim());
					group.setSubject(getInputSubject().trim());
					//createChatRoom();	
					if(chatHandler!=null){
					    chatHandler.onCreate(group);
					}
				}
				else{
					if(group.getPassword().length()>0 && !group.getPassword().equals(getInputPwd())){
						Window.alert("您输入的密码与您要加入的主题聊天室密码不符.");					
						return;
					}
					else{
						if(chatHandler!=null){
						   chatHandler.onJoin(group, getInputNiceName().trim());
						}
					}
				}				
				hide();//
			}
		});
	    closeButton.setStyleName("contentTxt");
	    confirmButton.setStyleName("contentTxt");
	}
    public void initData(String title , ChatGroup group , String actionType , String defNiceName){    	
	    this.actionType = actionType;
	    this.group = group;
	    this.defaultNiceName = defNiceName;
	    setText(title);
	    initInfoWidget();
    }

 
	public String getInputSubject(){
    	return ((TextBox)vInfo.getWidget(0, 1)).getText();
    }
    
    public String getInputPwd(){
    	return ((TextBox)vInfo.getWidget(1, 1)).getText();
    }

    public String getInputNiceName(){
    	return ((TextBox)vInfo.getWidget(2, 1)).getText();
    }
    
    private void initInfoWidget(){
    	if(vInfo!=null){
    		vInfo.clear();
    	}
    	vInfo.resize(3, 3);
	    vInfo.setStyleName("contentTxt");//不起作用？
    	vInfo.setHTML(0, 0, "主题");
    	TextBox subTxt = new TextBox();
    	
    	if(actionType.equals("join")){
    		subTxt.setReadOnly(true);        	
        	subTxt.setText(group.getSubject()+"("+group.getNiceName()+")");
    	}
    	vInfo.setWidget(0, 1,subTxt);
    	
    	vInfo.setHTML(1, 0, "密码");
    	vInfo.setWidget(1, 1,new TextBox());
    	vInfo.setHTML(1,2,"如果需要密码,请输入");

    	vInfo.setHTML(2, 0, "昵称");
    	TextBox niceNameTxt = new TextBox();
    	niceNameTxt.setText(defaultNiceName);
    	vInfo.setWidget(2, 1,niceNameTxt);
    	vInfo.setHTML(2,2,"请输入你在聊天室显示的名称");    	
    }
    
    @Override
	protected void onPreviewNativeEvent(NativePreviewEvent preview) {
		    super.onPreviewNativeEvent(preview);

		    NativeEvent evt = preview.getNativeEvent();
		    if (evt.getType().equals("keydown")) {
		      // Use the popup's key preview hooks to close the dialog when either
		      // enter or escape is pressed.
		      switch (evt.getKeyCode()) {
		        case KeyCodes.KEY_ENTER:
		        case KeyCodes.KEY_ESCAPE:
		          hide();
		          break;
		      }
		    }
    }

	public OpenBangChatHandler getChatHandler() {
		return chatHandler;
	}

	public void setChatHandler(OpenBangChatHandler chatHandler) {
		this.chatHandler = chatHandler;
	}

}
